//
//  LHRecommendSightDetailView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-15.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendSightDetailView.h"
#import "LHNetworkHandle.h"
#import "LHRecommendSightDetailModel.h"
#import "UIImageView+WebCache.h"

@interface LHRecommendSightDetailView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *sightPic;
@property (nonatomic, retain) UILabel *sightName;
@property (nonatomic, retain) UILabel *openTime;
@property (nonatomic, retain) UILabel *visitCount;
@property (nonatomic, retain) UILabel *splitLine1;
@property (nonatomic, retain) UILabel *splitLine2;
@property (nonatomic, retain) UILabel *splitLine3;
@property (nonatomic, retain) UILabel *recommendReson;
@property (nonatomic, retain) UILabel *recommendPic;
@property (nonatomic, retain) UILabel *introductionLabel;
@property (nonatomic, retain) UILabel *briefIntroduction;
@property (nonatomic, retain) UILabel *arriveLabel;
@property (nonatomic, retain) UILabel *detailArrive;
@property (nonatomic, assign) CGFloat f1;
@property (nonatomic, assign) CGFloat f2;
@property (nonatomic, assign) CGFloat f3;
@property (nonatomic, assign) CGFloat f4;


@property (nonatomic, retain) UIScrollView *myScrollView;
@end

@implementation LHRecommendSightDetailView



- (void)dealloc
{
    [_sightPic release];
    [_sightName release];
    [_openTime release];
    [_visitCount release];
    [_splitLine1 release];
    [_splitLine2 release];
    [_recommendReson release];
    [_introductionLabel release];
    [_briefIntroduction release];
    [_arriveLabel release];
    [_detailArrive release];
    [_splitLine3 release];

    
    [super dealloc];
}

/**
 *  初始化
 *
 *  @return self
 */

- (instancetype)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) {
        
        // 创建scrollView
        self.myScrollView = [[UIScrollView alloc] init];
        
        _myScrollView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        _myScrollView.showsHorizontalScrollIndicator = YES;
        
        _myScrollView.contentSize = CGSizeMake(0, 800);
        
        _myScrollView.delegate = self;
        
        [self addSubview:_myScrollView];
        
        [_myScrollView release];
        
        
        // 创建景点相框
        self.sightPic = [[UIImageView alloc] init];
        
        [_myScrollView addSubview:_sightPic];
        
        [_sightPic release];
        
        // 景点名
        self.sightName = [[UILabel alloc] init];
        
        [_myScrollView addSubview:_sightName];
        
        [_sightName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        
        [_sightName release];
        
        // 收藏按钮
        self.collectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        _collectionButton.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
        
        [_myScrollView addSubview:_collectionButton];
        
        // 拜访次数
        self.visitCount = [[UILabel alloc] init];
        
        [_visitCount setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        [_myScrollView addSubview:_visitCount];
        
        [_visitCount release];
        
        // 开放时间
        self.openTime = [[UILabel alloc] init];
        
        [_openTime setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
        
        _openTime.numberOfLines = 0;
        
        [_myScrollView addSubview:_openTime];
        
        [_openTime release];
        
        // 分割线1
        self.splitLine1 = [[UILabel alloc] init];
        
        _splitLine1.backgroundColor = [UIColor colorWithRed:0.877 green:0.923 blue:0.967 alpha:1.000];
        
        [_myScrollView addSubview:_splitLine1];
        
        [_splitLine1 release];
        
        
        // 推荐理由四字相框
        self.recommendPic = [[UILabel alloc] init];
        
        _recommendPic.backgroundColor = [UIColor colorWithRed:0.112 green:0.633 blue:0.423 alpha:1.000];
        
        _recommendPic.layer.cornerRadius = 17;
        _recommendPic.clipsToBounds = YES;
        
        _recommendPic.text = @"推荐理由";
        [_recommendPic setTextAlignment:NSTextAlignmentCenter];
        
        [_recommendPic setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_recommendPic setTextColor:[UIColor colorWithRed:0.783 green:0.945 blue:0.897 alpha:1.000]];
        
        [_myScrollView addSubview:_recommendPic];
        
        [_recommendPic release];
        
        // 推荐理由
        self.recommendReson = [[UILabel alloc] init];
        
        _recommendReson.numberOfLines = 0;
        
        [_recommendReson setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_myScrollView addSubview:_recommendReson];
        
        [_recommendReson release];
        
        // 分割线2
        self.splitLine2 = [[UILabel alloc] init];
        
        _splitLine2.backgroundColor = [UIColor colorWithRed:0.877 green:0.923 blue:0.967 alpha:1.000];
        
        [_myScrollView addSubview:_splitLine2];
        
        [_splitLine2 release];
        
        
        
        // 简介两个字label
        self.introductionLabel = [[UILabel alloc] init];
        
        [_introductionLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_myScrollView addSubview:_introductionLabel];
        
        [_introductionLabel release];
        
        // 简介
        
        self.briefIntroduction = [[UILabel alloc] init];
        
        [_briefIntroduction setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_myScrollView addSubview:_briefIntroduction];
        
        _briefIntroduction.numberOfLines = 0;
        
        [_briefIntroduction release];
        
        // 分割线3
        self.splitLine3 = [[UILabel alloc] init];
        
        _splitLine3.backgroundColor = [UIColor colorWithRed:0.877 green:0.923 blue:0.967 alpha:1.000];
        
        [_myScrollView addSubview:_splitLine3];
        
        [_splitLine3 release];
        
        
        // 如何到达label
        self.arriveLabel = [[UILabel alloc] init];
        
        [_myScrollView addSubview:_arriveLabel];
        
        [_arriveLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        [_arriveLabel release];
        
        // 具体到达方式
        self.detailArrive = [[UILabel alloc] init];
        
        [_myScrollView addSubview:_detailArrive];
        
        _detailArrive.numberOfLines = 0;
        
        [_detailArrive setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        
        [_detailArrive release];
        
        
    }
    
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    
    self.sightPic.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3);
    
    self.sightName.frame = CGRectMake(15, _sightPic.frame.origin.y + self.frame.size.height / 3 + 20, 250, 30);
    
    self.collectionButton.frame = CGRectMake(self.frame.size.width - 55, _sightPic.frame.origin.y + self.frame.size.height / 3 + 20, 40, 40);


    
    self.visitCount.frame = CGRectMake(15, _sightName.frame.origin.y + 40, 140, 20);
    
    self.openTime.frame = CGRectMake(15, _visitCount.frame.origin.y + 30, self.frame.size.width - 10,_f4);
    
    self.splitLine1.frame = CGRectMake(0, _openTime.frame.origin.y + _f4 + 10, self.frame.size.width, 3);
    
    self.recommendPic.frame = CGRectMake(self.frame.size.width / 2 - 50, _splitLine1.frame.origin.y + 33, 100, 30);
    
    self.recommendReson.frame = CGRectMake(15, _recommendPic.frame.origin.y + 50, self.frame.size.width - 30, _f1);
    
    self.splitLine2.frame = CGRectMake(0, _recommendReson.frame.origin.y + _f1 + 10, self.frame.size.width, 3);
    
    self.introductionLabel.frame = CGRectMake(15, _recommendReson.frame.origin.y + _f1 + 23, 40, 30);
    
    self.briefIntroduction.frame = CGRectMake(10, _introductionLabel.frame.origin.y + 50, self.frame.size.width - 30, _f2 );
    
    self.splitLine3.frame = CGRectMake(0, _briefIntroduction.frame.origin.y + _f2 + 30, self.frame.size.width, 3);
    
    self.arriveLabel.frame = CGRectMake(15, _splitLine3.frame.origin.y + 13, 80, 30);
    
    self.detailArrive.frame = CGRectMake(15, _arriveLabel.frame.origin.y + 50, self.frame.size.width - 20, _f3);

    self.myScrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    

    
    CGFloat dy = _detailArrive.frame.size.height + _detailArrive.frame.origin.y + 84;
    
    [_myScrollView setContentSize:CGSizeMake(0, dy)];

    
}


// 重写setter方法
- (void)setSightDetailModel2:(LHRecommendSightModel *)sightDetailModel2
{
    if (_sightDetailModel2 != sightDetailModel2) {
        
        [_sightDetailModel2 release];
        
        _sightDetailModel2 = [sightDetailModel2 retain];
        
    }
    // 给控件赋值
    
    [_sightPic sd_setImageWithURL:[NSURL URLWithString:sightDetailModel2.cover] placeholderImage:[UIImage imageNamed:@"word4"]];
    
    _sightName.text = sightDetailModel2.name;
    
    NSString *a = [NSString stringWithFormat:@"拜访次数:%ld", sightDetailModel2.visited_count];
    
    _visitCount.text = a;
    
    self.f4 = [[self class] heightWithText:sightDetailModel2.opening_time];
    NSString *b =[NSString stringWithFormat:@"开放时间:%@", sightDetailModel2.opening_time];
    
    _openTime.text = b;
    
    
     self.f1 = [[self class] heightWithText:sightDetailModel2.recommended_reason];
    _recommendReson.text = sightDetailModel2.recommended_reason;

    
    _introductionLabel.text = @"简介";
    
    self.f2 = [[self class] heightWithText:sightDetailModel2.sightDescription];
    _briefIntroduction.text = sightDetailModel2.sightDescription;
    

    
    
    _arriveLabel.text = @"如何到达";
    
    self.f3 = [[self class] heightWithText:sightDetailModel2.arrival_type];
    _detailArrive.text = sightDetailModel2.arrival_type;
    
}

// 文字自适应高度
+ (CGFloat)heightWithText:(NSString *)text
{
    
    // 创建一个字典保存文本属性
    // 保存文本文字大小
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica-Bold" size:17], NSFontAttributeName, nil];
    
    // 预设一个尺寸, 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(LHWidth - 20, 1000);
    
    // 根据文本信息获得一个CGRect
    // 参数1:尺寸范围  参数2:文本属性  参数3:按照什么方式获取Rect  参数4:nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;

    
    
    
}

// scrollview拖动时被调用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    
    
    if (y < 0) {
        
        _sightPic.frame = CGRectMake(y / 2, y, self.frame.size.width - y, self.frame.size.height / 3 - y);
    }
}




@end
